package io.github.lee.jel.global.di.http

import android.util.Log
import java.security.SecureRandom
import java.security.cert.X509Certificate
import javax.net.ssl.HostnameVerifier
import javax.net.ssl.SSLContext
import javax.net.ssl.SSLSession
import javax.net.ssl.SSLSocketFactory
import javax.net.ssl.X509TrustManager

object SSLSocketClient {
    val ssLSocketFactory: SSLSocketFactory
        get() = try {
            val sslContext = SSLContext.getInstance("SSL")
            sslContext.init(null, trustManager, SecureRandom())
            sslContext.socketFactory
        } catch (e: Exception) {
            throw RuntimeException(e)
        }

    val trustManager: Array<X509TrustManager>
        get() {
            return arrayOf(
                object : X509TrustManager {
                    //                    override fun checkClientTrusted(chain: Array<X509Certificate?>?, authType: String?) {}
//                    override fun checkServerTrusted(chain: Array<X509Certificate?>?, authType: String?) {}
//                    override fun getAcceptedIssuers(): Array<X509Certificate> {
//                        return arrayOf()
//                    }
                    override fun checkClientTrusted(
                        chain: Array<out X509Certificate>?,
                        authType: String?
                    ) {

                    }

                    override fun checkServerTrusted(
                        chain: Array<out X509Certificate>?,
                        authType: String?
                    ) {
                    }

                    override fun getAcceptedIssuers(): Array<X509Certificate> {
                        return arrayOf()
                    }
                }
            )
        }

    val hostnameVerifier: HostnameVerifier
        get() {
            return HostnameVerifier { hostname, _ ->

                //                override fun verify(s: String?, sslSession: SSLSession?): Boolean {
                //                    return true
                //                }
                Log.e("TAG", "hostname = $hostname")
                true
            }
        }

}